package com.fengye.cluster.impl;

import com.fengye.cluster.ClusterStrategy;
import com.fengye.common.IPHelper;
import com.fengye.model.ProviderService;

import java.util.List;

/**
 * 软负载hash算法，根据调用方ip的hash进行分配
 *
 * 仔细想想这里是有问题的（后面要改）
 *
 * @Author fengye
 * @Date 2023/2/15 11:01
 **/
public class HashClusterStrategyImpl implements ClusterStrategy {
    @Override
    public ProviderService select(List<ProviderService> providerServiceList) {
//        获取调用方IP
        String localIp = IPHelper.localIp();
        // 获取源地址对应的hashcode
        int hashCode = localIp.hashCode();
        // 获取服务列表的大小
        int size = providerServiceList.size();

        return providerServiceList.get(hashCode % size);
    }
}
